package com.qingcheng.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.Page;
import com.qingcheng.dao.TbSkuMapper;
import com.qingcheng.handler.InsElasticSearchHandler;
import com.qingcheng.pojo.goods.TbSku;
import com.qingcheng.service.TbSkuService;
import com.qingcheng.util.CacheKey;
import com.qingcheng.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import tk.mybatis.mapper.entity.Example;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;


/**
*
* @author Simon
* @date 2020/07/27
*/
@Service
@CacheConfig(cacheNames = "sku")
public class TbSkuServiceImpl implements TbSkuService {

    @Resource
    private TbSkuMapper tbSkuMapper;

    @Resource
    private RedisTemplate goodsRedisTemplate;

    @Resource
    private InsElasticSearchHandler es;

    /**
     * 定时任务每天晚上十一点过一秒更新内容
     * 缓存预热调用的方法，直接去缓存获取数据,缓存预热是为了解决
     * @return
     */
    @Override
    // @Scheduled(cron = "59 59 23 * * ?")
    public void saveSkuRedis() {
        // 存储到缓存
        if (goodsRedisTemplate.boundValueOps(CacheKey.SEARCH_TYPE).get() == null) {
            List<TbSku> tbSkus = tbSkuMapper.querySKuAndSpu();
            if (tbSkus != null) {
                goodsRedisTemplate.boundValueOps(CacheKey.SEARCH_TYPE).set(tbSkus);
                goodsRedisTemplate.boundValueOps(CacheKey.SEARCH_TYPE).expire(23, TimeUnit.HOURS);
            } else {
                TbSku sku = new TbSku();
                sku.setName("simon");
                sku.setBrandName("duncan");
                tbSkus.add(sku);
                goodsRedisTemplate.boundValueOps(CacheKey.SEARCH_TYPE).set(tbSkus);
            }
        }
    }


    @Override
    public List<TbSku> queryAll() {
        List<TbSku> tbSkus = tbSkuMapper.querySKuAndSpu();
        try {
            es.insOrDel(tbSkus,"ins");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public PageUtil<TbSku> findListAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSku> pages = (Page<TbSku>) tbSkuMapper.selectAll();
        PageUtil<TbSku> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
    }


    @Override
    public PageUtil<TbSku> pageQueryByAll(Integer pageNum, Integer pageSize, TbSku tbSku) {

        Example example=new Example(TbSku.class);
        Example.Criteria criteria = example.createCriteria();
        PageHelper.startPage(pageNum,pageSize);
        Page<TbSku> pages = (Page<TbSku>) tbSkuMapper.selectByExample(example);
        PageUtil<TbSku> pageUtil=new PageUtil<>();
        pageUtil.setRows(pages.getResult());
        pageUtil.setTotal(pages.getTotal());
        return pageUtil;
     }


    @Override
    public boolean saveTbSku(TbSku tbSku) {
        int insert = tbSkuMapper.insert(tbSku);
        if(insert>0){
            return true;
        }
        return false;
    }

    @Override
    public boolean updateTbSku(TbSku tbSku) {
        int i = tbSkuMapper.updateByPrimaryKeySelective(tbSku);
        if(i>0){
            return true;
        }
        return false;
    }


    @Override
    public boolean deleteById(Integer id) {
        int i = tbSkuMapper.deleteByPrimaryKey(id);
        if(i>0){
            return true;
        }
        return false;
    }

    @Override
    public TbSku findById(Integer id) {
        TbSku tbSku = tbSkuMapper.selectByPrimaryKey(id);
        return tbSku;
    }
}
